﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FastData
{
    public static class Extention
    {
 
        //static string TableName = "123";
        //static string nameOfTestVariable = MemberInfoGetting.GetMemberName(() => TableName);

        public static string ToString<T>(this IEnumerable<T> source, string separator)
        {
            return Conver.ConvertToString(source, separator);
        }


        public static string ToStringName<m,T>(this m source, Expression<Func<T>> memberExpression)
        {
            string aa = GetMemberName(memberExpression);
            return aa;
        }


        public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
        {
            MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
            return expressionBody.Member.Name;
        }

        public static DataPageSource ConverIntoDataPage(this DataTable srcDataTable, int pageSize)
        {
            try
            {
                int number = srcDataTable.Rows.Count / pageSize;
                if ((srcDataTable.Rows.Count - number * pageSize) > 0)
                    number++;
                DataTable[] dts = new DataTable[number];
                int i = 0;
                for (i = 0; i < number; i++)
                {
                    dts[i] = srcDataTable.Clone();
                    dts[i].TableName = string.Format("dt{0}", i);
                    DataRow[] dataRows = srcDataTable.Rows.OfType<DataRow>().Skip(i * pageSize).Take(pageSize).ToArray();
                    dts[i] = dataRows.CopyToDataTable();
                }
                DataSet dataSet = new DataSet();
                foreach (DataTable dt in dts)
                    dataSet.Tables.Add(dt);
                DataPageSource dataPageSource = new DataPageSource(dataSet);
                return dataPageSource;
            }
            catch (MySqlException e)
            {
                throw new Exception(e.Message);
            }
            catch (Exception e)
            {
                return new DataPageSource(10);
            }
        }
    }
}
